package com.cqc.jdk8.Jutil;

import com.cqc.other.methodContest.Person;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

public class OptionalTest {


    @Test
    public void f1() {
        Person p = null;
//        p = new Person("lucy",20);
        p = Optional.ofNullable(p).orElse(new Person("mike", 18));
        System.out.println(p.getUsername());

        List<Person> list = new ArrayList<>();
        list.add(null);
    }

    @Test
    public void f2() {
        String a = "a";
//        a = Optional.ofNullable(a).orElse(getDefaultValue());
//        System.out.println(a);

        a = Optional.ofNullable(a).orElseGet(() -> {
            return getDefaultValue();
        });
        a = Optional.ofNullable(a).orElseGet(() -> getDefaultValue());
        System.out.println(a);


        Optional<Object> empty = Optional.empty();
        Optional<Object> o = Optional.of(null);
    }

    private String getDefaultValue() {
        System.out.println("b");
        return "b";
    }

    /**
     * 判断person是否为空，不为空返回p.username属性，为空则返回默认值。
     */
    @Test
    public void f3() {
        Person p = new Person("mike", 18);
//        p = null;
//        String username = Optional.ofNullable(p).map(x -> x.getUsername()).orElse("cui");
//        String username = Optional.ofNullable(p).map(x -> x.getUsername()).orElseGet(() -> "cui");
//        System.out.println(username);
        //有值就不走 orElseGet()
//        String username = Optional.ofNullable(p).map(x -> x.getUsername()).orElseGet(() -> aOrElseGet());
//        System.out.println(username);
        //有值，也会先走aOrElse()
        String username = Optional.ofNullable(p).map(x -> x.getUsername()).orElse(aOrElse());
        System.out.println(username);
    }

    private String aOrElseGet() {
        System.out.println("orElseGet()");
        return "orElseGet";
    }

    private String aOrElse() {
        System.out.println("aOrElse()");
        return "aOrElse";
    }

    @Test
    public void f4() {
        String name = "cui";
        Optional<String> opt1 = Optional.ofNullable(null);
        Optional<Integer> opt2 = opt1.map(x -> x.length());
        System.out.println(opt2.orElse(0));
    }

    /**
     * map() filter() 判断String的长度 6<len<10
     */
    @Test
    public void f5() {
        String password = "1234569";
        Optional<String> opt = Optional.of(password);
        boolean present = opt.map(x -> x.toUpperCase()).filter(pwd -> pwd.length() > 6).filter(pwd -> pwd.length() < 10).isPresent();
        System.out.println(present);
        String result = opt.map(x -> x.toUpperCase()).filter(pwd -> pwd.length() > 6).filter(pwd -> pwd.length() < 10).orElse("");
        System.out.println(result);
    }

    @Test
    public void f6() {
        String password = "1234569";
        Optional<String> opt = Optional.of(password);
        Function<String, String> fun = x -> x.toUpperCase();
        Predicate<String> pred1 = pwd -> pwd.length() > 6;
        Predicate<String> pred2 = pwd -> pwd.length() < 10;
        Predicate<String> pred3 = pwd -> pwd.equals("1234569");

        boolean present = opt.map(fun).filter(pred1.and(pred2).and(pred3)).isPresent();
        System.out.println(present);
    }

    @Test
    public void f7() {
        String a = "0005";
//        a = null;
        String result = Optional.ofNullable(a).map(x -> (String.format("%04d", 1 + Integer.parseInt(x)))).orElse("0001");
        System.out.println("result=" + result);
        String result2 = Optional.ofNullable(a).map(x -> (String.format("%011d", 1 + Integer.parseInt(x)))).orElse("00000000001");
        System.out.println("result2=" + result2);
    }

    @Test
    public void f8() {
        //原文链接：https://blog.csdn.net/yangguoqi/article/details/106817137
        //orElseThrow当没有值时，走该方法。
        String requestId = "ad";
        Map<String, String> taskMap = new ConcurrentHashMap<>();
        Optional.ofNullable(taskMap)
                .filter(t -> t.containsKey(requestId))
                .orElseThrow(() -> new IllegalArgumentException(String.format("requestId=%s is existing", requestId)));
        System.out.println("111");
    }


}
